package com.niit.service.impl;

import java.util.List;

import com.niit.bean.Question;
import com.niit.dao.QuestionDao;
import com.niit.dao.impl.QuestionDaoImpl;
import com.niit.service.QuestionService;
import com.niit.utils.Page;

public class QuestionServiceImpl implements QuestionService {
	
	private QuestionDao dao = new QuestionDaoImpl();
	@Override
	public int add(Question t) {
		return dao.add(t);
	}

	@Override
	public int delete(Question t) {
		return dao.delete(t);
	}

	@Override
	public int update(Question t) {
		return dao.update(t);
	}

	@Override
	public Page<Question> getPage(String pagenum, String url) {
		int total = dao.findTotal();
		Page<Question> page = null;
		if(pagenum!=null){
			page = new Page<Question>(total, Integer.parseInt(pagenum));
		}else{
			page = new Page<Question>(total, 1);
		}
		List<Question> list = dao.getPageByClassify(null, page.getStartIndex(), page.getEndIndex());
		page.setUrl(url);
		page.setList(list);
		return page;
	}
	@Override
	public Question findByQuesId(String quesId) {
		return dao.findByQuesId(quesId);
	}

	@Override
	public List<Object[]> findQuesAndCount4Reply() {
		return dao.findQuesAndCount4Reply();
	}

	@Override
	public Page<Question> getPageByClassify(String classify, String pagenum,
			String url) {
		Page<Question> page = null;
		int total = 0;
		if(classify==null){
			//查出所有，不再按分类
			total = dao.findTotal();
		}else{
			total = dao.findTotalByClassify(classify);
		}
		if(pagenum == null){
			page = new Page<Question>(total,1);
		}else{
			page = new Page<Question>(total, Integer.parseInt(pagenum));
		}
		List<Question> list = dao.getPageByClassify(classify, page.getStartIndex(), page.getEndIndex());
		page.setList(list);
		page.setUrl(url);
		return page;
	}

	@Override
	public String findMaxQuesId() {
		return dao.findMaxQuesId();
	}

	@Override
	public int findTotal() {
		return dao.findTotal();
	}
}
